﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using W.Carrier.Api.Application.Command;
using W.Carrier.Domain;

namespace W.Carrier.Api.Controllers
{
    /// <summary>
    /// 承运商控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class CarrierController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediatR;
        private readonly ILogger<CarrierController> _logger;
        public CarrierController(IMediator mediator, ILogger<CarrierController> logger)
        {
            _mediatR = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 添加承运商信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddCarrierAsync(CarrierCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用添加承运商信息方法");
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
                command.CreateName = name;
                command.userid = Convert.ToInt32(uid);
                command.officeId = sid;
                result.Code= await _mediatR.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用添加承运商信息方法时出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 修改承运商信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateCarrierAsync(CarrierUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用修改承运商信息方法");
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
                command.CreateName = name;
                command.userid = Convert.ToInt32(uid);
                command.officeId = sid;
                command.UpdateName = name;
                result.Code = await _mediatR.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用添加承运商信息方法时出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 删除承运商信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteCarrierAsync(CarrierDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用删除承运商信息方法");
                result.Code = await _mediatR.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用删除承运商信息方法时出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 批量删除承运商信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteRangeCarrierAsync(CarrierDeleteRangeCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用批量删除承运商信息方法");
                result.Code = await _mediatR.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用批量删除承运商信息方法时出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
    }
}
